package cn.dxj1016.chat;

import java.net.DatagramPacket;
import java.net.DatagramSocket;


public class UdpReceiveDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket datagramSocket = new DatagramSocket(6666);
        while (true) {
            //        准备接收包裹
            byte[] bytes = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
            datagramSocket.receive(packet);//阻塞式接收包裹
//        断开连接 bye
            byte[] data = packet.getData();
            String s = new String(data, 0, data.length);

            System.out.println(s);

            if (s.equals("bye")) {
                break;
            }
        }
        datagramSocket.close();
        }

}
